/**
 * @file   ModBusiness.java
 * @author Enrique Madrid <e.madrid@lpcsite.com>
 * @date   Tue Sep 18 10:48:33 2007
 *
 * @brief
 *
 *
 */
package sim.framework.modules.business;

import sim.framework.*;
import sim.framework.dialog.Dialog;
import sim.framework.dialog.DlgNotify;

public abstract class ModBusiness extends Module {
    
    protected static final int MOD_NONE = 0;
    protected static final int MOD_HEADER = -1;
    protected static final int MOD_DETAIL = -2;
    protected static final int MOD_RESUME = -3;
    protected static final int MOD_ADMINS = -4;
    
    
    private int m_Return = ModBusiness.MOD_NONE;
    
    // Variables del resumen
    private DlgNotify m_DlgResume;
    private StringBuffer m_TextBuffer = new StringBuffer(64);
    
    public ModBusiness(int id, Simlet s) {
        super(id, s);
    }
    
    public void work(final Msg msg) {
        switch (msg.getWParam()) {
            case STATE_INIT:
                workInit(msg);
                break;
            case STATE_NEXT:
                workNext(msg);
                break;
            case STATE_BACK:
                workBack(msg);
                break;
        }
    }
    
    private void workInit(final Msg msg) {
        DocHeader docHeader;
        //#ifdef DEBUG_MODBUSINESS
        //# System.out.println("ModBusiness::workInit() ENTERED");
        //#endif
        if (execModule(MOD_HEADER)) {
            docHeader = (DocHeader)getDocument();
            if (docHeader != null) {
                docHeader.setCreateAge();
            }
            return;
        }
        execBack();
        //#ifdef DEBUG_MODBUSINESS
        //# System.out.println("ModBusiness::workInit() EXITING");
        //#endif
    }
    
    private void workNext(final Msg msg) {
        switch (m_Return) {
            case MOD_HEADER:
                if (execModule(MOD_DETAIL)) {
                    break;
                }
            case MOD_DETAIL:
                m_Return = MOD_RESUME;
                // ... borrar el contenido de. buffer temporal
                appendText(null);
                // llamar al callback businessResume().
                businessResume();
                // Si se editó en el buffer temporal ...
                if (m_TextBuffer.length() > 0) {
                    // se crea el diálogo
                    bussinessCreateDlg();
                    // ... mostrarlo en el cuadro en la interfaz gráfica.
                    m_DlgResume.setText(Dialog.CTRL_MAIN, m_TextBuffer);
                    setGui(m_DlgResume);
                    break;
                }
            case MOD_RESUME:
                if (execModule(MOD_ADMINS)) {
                    break;
                }
            case MOD_ADMINS:
                execNext();
        }
    }
    
    private void workBack(final Msg msg) {
        switch (m_Return) {
            case MOD_ADMINS:
                if(execModule(MOD_DETAIL)) {
                    break;
                }
            case MOD_RESUME:
                if(execModule(MOD_DETAIL)) {
                    break;
                }
            case MOD_DETAIL:
                if (execModule(MOD_HEADER)) {
                    break;
                }
            case MOD_HEADER:
                execBack();
                break;
        }
    }
    
    private boolean execModule(int id) {
        Module m = null;
        
        //#ifdef DEBUG_MODBUSINESS
        //# System.out.println("ModBusiness::execModule() ENTERED");
        //#endif
        
        m = createModule(id);
        if (m != null) {
            m_Return = id;
            execQueue(this, this);
            exec(m);
        }
        
        //#ifdef DEBUG_MODBUSINESS
        //# System.out.println("ModBusiness::execModule() EXITING");
        //#endif
        
        return (m != null);
    }
    
    protected void bussinessCreateDlg() {
        // Crear el diálogo para mostrar información.
        m_DlgResume = (DlgNotify)DlgNotify.create(m_Simlet,
                "",
                null,
                getLabel(Resource.LBL_CTRL_BACK),
                getLabel(Resource.LBL_CTRL_NEXT));
    }
    
    public boolean event(final Msg msg) {
        boolean ret = false;
        switch (msg.getEventCode()) {
            case EventListener.COMMAND:
                switch (msg.getWParam()) {
                    case Dialog.CTRL_BUTTON_NEXT:
                        workBack(msg);
                        ret = true;
                        break;
                    case Dialog.CTRL_BUTTON_BACK:
                        workNext(msg);
                        ret = true;
                        break;
                }
        }
        return ret;
    }
    
    protected final void appendText(final String string) {
        // Si el parámetro es nulo ...
        if (string == null) {
            // Borrar el buffer temporal
            m_TextBuffer.setLength(0);
        } else {
            // En caso contrario añadir text
            m_TextBuffer.append(string);
        }
    }
    
    protected void businessResume() {
        appendText(null);
    }
}

